#Copyright 2009-2010 by Ward, David J ( pymethius@ominian.net )
from logging import getLogger
log = getLogger(__name__)

import ConfigParser
import os, sys
from copy import copy

class IoCConfig(object):    
    
    def __init__(self):
        self.configs = {}
    
    
    def provides(self, configFile, args = None):
        if not os.path.isfile(configFile): raise ValueError("%s did not resolve " % configFile)
        
        here = os.getcwd()
        name = os.path.basename(configFile).rsplit(".",1)[0]
        
        
        temp = self.configs[name] = {}        
        qp = ConfigParser.ConfigParser(defaults={"here":here})
        qp.read(configFile)        
        for section in qp.sections():
            temp[section] = dict(qp.items(section))
        
        del(qp) #Doesn't hurt to kill it early

    def list(self):
        return self.configs.keys()

    def require(self, name):
        """
            Returns a copy of a configuration element.  Highly important to remember that, usage should be sparingly
        """
        try:
            return copy(self.configs[name])
        except KeyError:            
            log.exception("Missing required %s in %s" % (name, self.list(),) )

_config = IoCConfig()

provides = _config.provides
list = _config.list
require = _config.require
